package com.limit.control.admin;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.UploadFile;
import com.limit.aop.AdminPerimissionAop;
import com.limit.constant.Constant;
import com.limit.model.Actor;
import com.limit.model.ActorModule;
import com.limit.model.Module;
import com.limit.utils.PropertiesUtil;

@Before(AdminPerimissionAop.class)
public class BaseController extends Controller {

	protected final int pageSize = 12;
	
	protected long id = 0L;
	
	/** 
	 * 为ID注入值,若没有传值,则跳转至error_param.html页面
	 */
	protected void _commonSetId() {
		long temp = this.getParaToLong("id", 0L);
		id = temp <=0 ? id : temp;
	}
	
	/**
	 * 刷新缓存
	 */
	public void _refreshPermissionCache(String actorId , String username){
		CacheKit.remove(Constant.CacheActorPermission, username);
		List<Map<String,Object>> actorPerimissionList = new ArrayList<Map<String,Object>>();
		Actor actor = Actor.dao.findById(actorId);
		if(null!=actor){
			List<ActorModule> umlist = actor.getAdminModuleList();
			if(null!=umlist && umlist.size()>0){
				for(ActorModule um : umlist){
					Map<String,Object> map = new HashMap<String,Object>();
					Module m = um.getModule();
					map.put("id", m.get("id"));
					map.put("pid", m.get("pid"));
					map.put("url", m.get("url"));
					map.put("name", m.get("menu_name"));
					actorPerimissionList.add(map);
				}
				CacheKit.put(Constant.CacheActorPermission, username, actorPerimissionList);
			}
		}
	}
	
	/**
	 * 重命名
	 */
	protected String _uploadFile(UploadFile uploadFile, String newFileName , String savePath) {
		//取到上传的文件
		File file = uploadFile.getFile();
		int len = uploadFile.getOriginalFileName().length();
		//获取原文件所在路径
		String url = file.getPath().substring(0, file.getPath().length()-len);
		String path =  "/upload/"+savePath;
		//重命名原文件
		file.renameTo(new File(url+newFileName));
		return path;
	}
	
}
